import utils from '../../../utils/util'
Page({
  data: {
    modalName: null,
    user: null,
    lover: null,
    event: "",
    date: '',
    anniversary: [],
    index: 0 //全部纪念日数组的下标
  },
  showModal(e) {
    var date = new Date
    const year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()
    if(month < 10 ) month = '0' + month 
    if(day < 10 ) day = '0' + day 

    this.setData({
      date: year + '年' + month + '月' + day + '日',  // 日期选择默认当前日期
      event: "",
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  // ListTouch触摸开始
  ListTouchStart(e) {
    this.setData({
      ListTouchStart: e.touches[0].pageX
    })
  },

  // ListTouch计算方向
  ListTouchMove(e) {
    this.setData({
      ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
    })
  },

  // ListTouch计算滚动
  ListTouchEnd(e) {
    if (this.data.ListTouchDirection == 'left') {
      this.setData({
        modalName: e.currentTarget.dataset.target
      })
    } else {
      this.setData({
        modalName: null
      })
    }
    this.setData({
      ListTouchDirection: null
    })
  },
  onLoad() {
    var user = wx.getStorageSync('user')
    var lover = wx.getStorageSync('lover')
    this.getAnniversary(user._openid, 0).then(res => { // 查找该用户的所有纪念日
      this.toDay(res.data)
      this.setData({
        user: user,
        lover: lover,
        anniversary: res.data
      })
    })
  },
  getinput(e) {
    this.setData({
      event: e.detail.value
    })
  },
  DateChange(e) {
    var arr = e.detail.value.split("-");
    this.setData({
      date: arr[0] + '年' + arr[1] + '月' + arr[2] + '日',
    });
  },
  addAnniversary() {
    if (this.data.event == "") {
      wx.showToast({
        icon: "none",
        title: '事件不能为空',
      })
      return
    }
    wx.showLoading({
      title: '正在添加'
    })
    wx.cloud.database().collection('anniversary').add({
      data: {
        lover_openid: this.data.lover._openid,
        event: this.data.event,
        date: this.data.date
      }
    }).then(res => {
      wx.hideLoading()
      wx.showToast({
        title: '添加成功',
      })
      this.getAnniversary(this.data.user._openid, 0).then(res => {
        this.toDay(res.data)
        this.setData({
          modalName: null,
          anniversary: res.data
        })
      })

    })
  },
  edit_show(e) {
    this.setData({
      modalName: "resetModal",
      event: this.data.anniversary[e.currentTarget.dataset.index].event,
      date: this.data.anniversary[e.currentTarget.dataset.index].date,
      index: e.currentTarget.dataset.index
    })
  },
  edit() {
    wx.showLoading({
      title: '修改中'
    })
    this.data.anniversary[this.data.index].event = this.data.event
    this.data.anniversary[this.data.index].date = this.data.date
    wx.cloud.callFunction({
      name: "anniversary",
      data: {
        type: 'update',
        anniversary: this.data.anniversary[this.data.index]
      }
    }).then(res => {
      this.getAnniversary(this.data.user._openid, 0).then(res => {
        this.toDay(res.data)
        this.setData({
          anniversary: res.data,
          modalName: null
        })
        wx.hideLoading()
        wx.showToast({
          title: '修改成功',
        })
      })
    })
  },
  delete(e) {
    wx.showLoading({
      title: '正在删除'
    })
    wx.cloud.callFunction({
      name: "anniversary",
      data: {
        type: "delete",
        anniversary: this.data.anniversary[e.currentTarget.dataset.index]
      }
    }).then(res => {
      this.getAnniversary(this.data.user._openid, 0).then(res => {
        this.toDay(res.data)
        this.setData({
          anniversary: res.data
        })
        wx.hideLoading()
        wx.showToast({
          title: '删除成功',
        })
      })
    })
  },
  getAnniversary(_openid, skiplength) {
    const db = wx.cloud.database()
    const _ = db.command
    return db.collection('anniversary').where(_.or([
      { _openid: _openid },
      { lover_openid: _openid }
    ]))
    .skip(skiplength)
    .get()
  },
  onPullDownRefresh(){
    wx.showLoading({
      title: '加载中',
    })
    this.getAnniversary(this.data.user._openid,0).then(res=>{
      this.toDay(res.data)
      this.setData({
        anniversary: res.data
      })
      wx.stopPullDownRefresh()
      wx.hideLoading()
    })
  },
  onReachBottom(){
    wx.showLoading({
      title: '加载中',
    })
    this.getAnniversary(this.data.user._openid, this.data.anniversary.length).then(res=>{
      if(res.data.length <= 0){
        wx.hideLoading()
        wx.showToast({
          icon: "none",
          title: '没有更多数据啦',
        })
        return 
      }
      this.toDay(res.data)
      this.setData({
        anniversary: this.data.anniversary.concat(res.data)
      })
      wx.hideLoading()
    })
  },
  toDay(anniversary){  //计算每个日期还有多少天
    let start_num = Date.parse(new Date(utils.formatTime3(new Date)))
    anniversary.forEach(item => {
      let month = item.date.substr(5, 2)
      let day = item.date.substr(8, 2)
      var year = new Date().getFullYear()
      let end_num = Date.parse(new Date(year + '/' + month+'/'+day)) //目标时间戳
      var usedTime =  end_num - start_num; //两个时间戳相差的毫秒数
      let days
      if( year % 4 == 0) days = 365
      else days = 365
      item.today = ((Math.floor(usedTime / (24 * 3600 * 1000)))+days) % days ;
    });
  },
})